<?php
/**
 * 验证类方法
 * 2021-02-01
 */

namespace App\Rules;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RuleFun {
    /**
     * 接收用户传递信息post
     * @param $rule 验证规则数组 [ 'name' => 'required', 'password' => 'required', 'captcha' => 'required|captcha', ]
     * @param $ruleMsg 验证规则返回的信息组，与$rule数组中的key对应 [ 'name.required' => '请输入登录名', 'password.required' => '请输入密码', 'captcha.required' => '请输入验证码', 'captcha.captcha' => '验证码有误', ]
     */
    public static function params($request,$rule,$ruleMsg){
        $validator = Validator::make($request->all(), $rule,$ruleMsg);
        if ($validator->fails()) {
            return ["code"=>-1,"data"=>$validator];
        }
        //检查
        $params = $validator->getData();
        return $params;
    }

    /**
     * 接收用户传递信息get
     * @param $rule 验证规则数组 [ 'name' => 'required', 'password' => 'required', 'captcha' => 'required|captcha', ]
     * @param $ruleMsg 验证规则返回的信息组，与$rule数组中的key对应 [ 'name.required' => '请输入登录名', 'password.required' => '请输入密码', 'captcha.required' => '请输入验证码', 'captcha.captcha' => '验证码有误', ]
     */
    public static function getParams($files,$rule,$ruleMsg){
        $validator = Validator::make($files, $rule,$ruleMsg);
        if ($validator->fails()) {
            return ["code"=>-1,"data"=>$validator];
        }
        //检查
        $params = $validator->getData();
        return $params;
    }
}
